package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.Menu;
import com.dk.pojo.TPerson;
import com.dk.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menu")
public class MenuController {

    @Reference
    private MenuService menuService;


    //后台管理查询
    @RequestMapping("getInfo")
    @ResponseBody
    public List<Map<String,Object>> getInfo(HttpServletRequest request){
//        TPerson tPerson  = (TPerson) request.getSession().getAttribute("loginNews");
        TPerson person  =(TPerson) SecurityUtils.getSubject().getPrincipal();
        List<Map<String,Object>> list = menuService.getInfo(person.getPid());
        System.out.println(person.getPname()+"登录");
        return list;
    }

    @RequestMapping("show")
    public String show(){
        return "menu/show";
    }

    //菜单查询
    @RequestMapping("getZtreeMenu")
    @ResponseBody
    public List<Map<String,Object>> getZtreeMenu(){
        List<Map<String, Object>> list = menuService.getZtreeMenu();
        return list;
    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "menu/add";
    }

    @RequestMapping("add")
    @ResponseBody
    public Integer add(Menu menu){
        Integer i = menuService.insertInfo(menu);
        return i;
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Integer parentid, Model model){
        Menu menu = menuService.toUpdate(parentid);
        model.addAttribute("menu",menu);
        return "menu/update";
    }

    @RequestMapping("update")
    @ResponseBody
    public boolean update(Menu menu){
        boolean i = menuService.updateInfo(menu);
        return i;
    }

    @RequestMapping("deleteInfo")
    @ResponseBody
    public Integer deleteInfo(Integer parentid){
        return menuService.deleteInfo(parentid);
    }

    @RequestMapping("getZtreeMenuChecked")
    @ResponseBody
    public List<Map<String,Object>> getZtreeMenuChecked(@RequestParam("roleId")Integer roleId){
        List<Map<String,Object>> list = menuService.getChecked(roleId);
        return list;

    }

}
